package com.jbm.jbmsupplier.presenters;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.jbm.jbmsupplier.modes.PledgeInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.PledgeViews;
import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.IOrderCtrl;
import com.jbm.onehome.iface.IPayCtrl;
import com.jbm.onehome.iface.ISeller;
import com.jbm.onehome.iface.IorderEntity;
import com.jbm.onehome.iface.PaybackEntity;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 家帮忙 on 2017/7/10.
 */

public class PledgeLists implements PledgeInterface {
    private Context context;
    private PledgeViews pledgeViews;
    private List<PaybackEntity> arr;
    private List<IorderEntity> arrlist;
    private NetIntent netintent;
    private final int PLEDGE = 0001;
    DialogLoading dialogloading;
    private int pages;
    /**
     * tags
     * ==1所有金额
     * ==2月结金额
     */
 private  int  tags;
    public PledgeLists(Context context, PledgeViews pledgeViews) {
        this.context = context;
        this.pledgeViews = pledgeViews;
    }

    /**
     * 获取钱包的总金额
     * 之后在获取明细数据
     */
    @Override
    public void getInit() {
        arr = new ArrayList();
        netintent = new NetIntent(handler);
        LogadingAmount();//获取用户的所有金额
    tags=1;
    }

    @Override
    public void getYueJieList() {
        arrlist=new ArrayList<>();
        LogadingAmount();
        tags=2;
    }

    /**
     * 上下拉动加载更多获取数据
     * @param page
     */
    @Override
    public void getRequData(int page) {//
        this.pages = page;
        netintent.getPayListS(PLEDGE, page);
        if (dialogloading!=null){
            dialogloading.show();
        }else{
            dialogloading = new DialogLoading(context, "加载中....", true);
            dialogloading.show();
        }
//        dialogloading.onWindowFocusChanged(false);

    }

    @Override
    public void LogadingAmount() {//加载用户的所有金额
        Observable.create(new Observable.OnSubscribe<ISeller>() {
            @Override
            public void call(Subscriber<? super ISeller> subscriber) {
                ISeller ymoney = OHInterface.getG_server().GetMyselfSeller();
                subscriber.onNext(ymoney);
            }
        }).observeOn(AndroidSchedulers.mainThread())//zza这个方法是主线程交给观察者来使用
                .subscribeOn(Schedulers.io())//这个方法是子线程交给被观察者来使用
                .subscribe(new Observer<ISeller>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ISeller iSeller) {
                        if (iSeller.m_code == 200) {
                            pledgeViews.getSuccedje(iSeller,tags);
                        }
                    }
                });


    }

    @Override
    public void getRequlist(final int page) {
        pages=page;
        if (dialogloading!=null){
            dialogloading.show();
        }else{
            dialogloading = new DialogLoading(context, "加载中....", true);
            dialogloading.show();
        }
        Observable.create(new Observable.OnSubscribe<IOrderCtrl>() {
            @Override
            public void call(Subscriber<? super IOrderCtrl> subscriber) {
                IOrderCtrl ymoney = OHInterface.getG_server().getUnPayList(page,10);
                subscriber.onNext(ymoney);
            }
        }).observeOn(AndroidSchedulers.mainThread())//zza这个方法是主线程交给观察者来使用
                .subscribeOn(Schedulers.io())//这个方法是子线程交给被观察者来使用
                .subscribe(new Observer<IOrderCtrl>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(IOrderCtrl ipay) {

                            if (ipay.m_code == 200) {
                                if (pages == 0) {
                                    if (ipay.orderList != null) {
                                        arrlist.clear();
                                        arrlist.addAll(ipay.orderList);
                                        pledgeViews.getListData(arrlist, 1);
                                        pledgeViews.getSucced(dialogloading);//加载成功
                                    }else {
                                        pledgeViews.getSucced(dialogloading);//加载成功
                                    }
                                } else {
                                    if (ipay.orderList != null) {
                                        arrlist.addAll(ipay.orderList);
                                        pledgeViews.getListData(arrlist, 2);
                                        pledgeViews.getSucced(dialogloading);//加载成功
                                    } else {
                                        pledgeViews.getUpdata();
                                        pledgeViews.getSucced(dialogloading);//加载成功
                                    }
                                }

                            } else {
                                pledgeViews.getError(ipay.m_info, dialogloading);//加载失败
                            }
                        }
                });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PLEDGE:
                    IPayCtrl ipay = (IPayCtrl) msg.obj;
                    if (ipay.m_code == 200) {
                        if (pages == 0) {
                            if (ipay.paybackEntityList != null) {
                                arr.clear();
                                arr.addAll(ipay.paybackEntityList);
                                pledgeViews.getData(arr, 1);
                                pledgeViews.getSucced(dialogloading);//加载成功
                            }else {
                                pledgeViews.getSucced(dialogloading);//加载成功
                            }
                        } else {
                            if (ipay.paybackEntityList != null) {
                                arr.addAll(ipay.paybackEntityList);
                                pledgeViews.getData(arr, 2);
                                pledgeViews.getSucced(dialogloading);//加载成功
                            } else {
                                pledgeViews.getUpdata();
                                pledgeViews.getSucced(dialogloading);//加载成功
                            }
                        }

                    } else {
                        pledgeViews.getError(ipay.m_info, dialogloading);//加载失败
                    }

                    break;
            }
        }
    };

}
